package yunquan.wang.modules.base.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import yunquan.wang.base.dao.DaoUtils;
import yunquan.wang.base.dao.springdata.BaseRepository;
import yunquan.wang.base.service.impl.BaseServiceImpl;
import yunquan.wang.modules.base.dao.GroupTypeDao;
import yunquan.wang.modules.base.entity.GroupType;
import yunquan.wang.modules.base.service.GroupTypeService;
/**
 *  GroupType Service层 实现
 *  @author 王云权  wangyunquan@gmail.com
 *  用生成器 生成于 2011-09-30 22:37
 *
 */
@Service("groupTypeService")
@Transactional(value = "jpaTransaction")
public class GroupTypeServiceImpl extends BaseServiceImpl<GroupType>
		implements GroupTypeService {
	private GroupTypeDao groupTypeDao;
	@Resource 
	public void setDao(GroupTypeDao groupTypeDao) {
		this.groupTypeDao = groupTypeDao;
	}
	public BaseRepository<GroupType, String> getDao() {
		return groupTypeDao;
	}
	@Override
	public List<GroupType> getNotTopGroupTypes() {
		//除第一层机构外的所有
		Specification spec=Specifications.where( DaoUtils.ne("level", 0));
		return groupTypeDao.findAll(spec);
	}
	public GroupType save(GroupType entity)
	{
		if(entity.getParent()!=null)
		{
		GroupType parent=get(entity.getParent().getId());
		entity.setLevel(parent.getLevel()+1);
		}
		else
		{
			entity.setLevel(0);
		}
		super.save(entity);
		return entity;
	}

}
